package life.mahu.community.controller;

import life.mahu.community.exception.CustomizeErrorCode;
import life.mahu.community.dto.CommentCreateDTO;
import life.mahu.community.dto.CommentDTO;
import life.mahu.community.dto.ResultDTO;
import life.mahu.community.dto.UserCommentDTO;
import life.mahu.community.enums.CommentTypeEnum;
import life.mahu.community.model.Comment;
import life.mahu.community.model.User;
import life.mahu.community.model.UserComment;
import life.mahu.community.service.CommentService;
import life.mahu.community.util.SensitiveWordFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private SensitiveWordFilter sensitivewordFilter;

    /**
     * 点赞/取消点赞
     * @param userCommentDTO
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/commentLike", method = RequestMethod.POST)
    public ResultDTO commentLike(@RequestBody UserCommentDTO userCommentDTO,
                                 HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return ResultDTO.errorOf(CustomizeErrorCode.NO_LOGIN);
        }

        UserComment userC = new UserComment();
        userC.setUserId(user.getId());
        userC.setCommentId(userCommentDTO.getCommentId());
        boolean isTrue = commentService.insertOrUpdateUC(userC, userCommentDTO.getType());

        if (!isTrue) {
            ResultDTO.errorOf(CustomizeErrorCode.SYS_ERROR);
        }

        return ResultDTO.okOf();
    }

    @ResponseBody
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public Object post(@RequestBody CommentCreateDTO commentCreateDTO,
                       HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return ResultDTO.errorOf(CustomizeErrorCode.NO_LOGIN);
        }

        //如果用户被禁言了
        if(user.getType()==-1){
            return ResultDTO.errorOf(CustomizeErrorCode.NO_WORDS_OPERATION);
        }

        //敏感词过滤
        if(sensitivewordFilter.isContaintSensitiveWord(commentCreateDTO.getContent(),1) ){
            return ResultDTO.errorOf(CustomizeErrorCode.ILLEGAL_COMMENT);
        }

        if (commentCreateDTO == null || StringUtils.isBlank(commentCreateDTO.getContent())) {
            return ResultDTO.errorOf(CustomizeErrorCode.CONTENT_IS_EMPTY);
        }

        Comment comment = new Comment();
        comment.setParentId(commentCreateDTO.getParentId());
        comment.setContent(commentCreateDTO.getContent());
        comment.setType(commentCreateDTO.getType());
        comment.setGmtModified(System.currentTimeMillis());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setCommentator(user.getId());
        comment.setLikeCount(0L);
        commentService.insert(comment, user);
        return ResultDTO.okOf();
    }

    @ResponseBody
    @RequestMapping(value = "/comment/{id}", method = RequestMethod.GET)
    public ResultDTO<List<CommentDTO>> comments(@PathVariable(name = "id") Long id) {
        List<CommentDTO> commentDTOS = commentService.listByTargetId(id, CommentTypeEnum.COMMENT);
        return ResultDTO.okOf(commentDTOS);
    }
}
